<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input name="name" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">类型</label>
                        <div class="layui-input-inline mr0">
							<select  name="type" lay-filter="type" >
								<option value="">--ALL--</option>
								<option value="Points">点集</option>
								<option value="Polylines">线集</option>
								<option value="Polygons">面集</option>
							</select>
                        </div>
                    </div>                    

                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSearchBizLayer" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnAddBizLayer" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button id="btnUploadKmlBtn" class="layui-btn icon-btn"><i class="layui-icon">&#xe681;</i>Kml导入</button>
                    </div>
                </div>
            </div>
            <table class="layui-table" id="tableBizLayer" lay-filter="tableBizLayer"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarAuth">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelLayer">
    <form id="modelLayerForm" lay-filter="modelLayerForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标识</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入标识" type="text" class="layui-input" maxlength="50"
                       />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
				<select id="lsyerTypeSelectId" name="type" lay-filter="type" lay-verify="required" required>
					<option value="">--请选择--</option>
					<option value="Points">点集</option>
					<option value="Polylines">线集</option>
					<option value="Polygons">面集</option>
				</select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">序号</label>
            <div class="layui-input-block">
                <input name="sort" placeholder="请输入序号" type="number" class="layui-input" min="0" max="1000"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitLayer" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
layui.extend({
    treeSelect: '/treeSelect/treeSelect'
}).use(['layer', 'form', 'table', 'admin', 'treetable', 'config','upload','treeSelect'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        var treetable = layui.treetable;
        var config = layui.config;
        var treeSelect=layui.treeSelect;
        var upload = layui.upload;
        
        var insTb = table.render({
        	id:'tableBizLayer',
            elem: '#tableBizLayer',
            url: '#(ctx)/biz/layer/listData',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                {type:'radio'},
                {field: 'name', title: '名称', minWidth: 200},
                {field: 'code', title: '标识'},
                {field: 'type', title: '类型'},
                {field: 'sort', title: '序号', align: 'center'},
                {templet: '#tableBarAuth', title: '操作', align: 'center', minWidth: 120}
            ]],
        	done: function(res, curr, count){
          	}
        });
        form.render();
        
        form.on('submit(formSearchBizLayer)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });
        
        // 添加
        $('#btnAddBizLayer').click(function () {
            showEditModel();
        });
        
        // 工具条点击事件
        table.on('tool(tableBizLayer)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(data);
            }
            else if (layEvent === 'Kml') { // 删除
            	ImportKml(data);
            }
        });

		var uploadInst=upload.render({
			elem: '#btnUploadKmlBtn',
			url: '#(ctx)/biz/layer/uploadKml',
			accept: 'file',
			exts: 'kml',
			auto: true,
			before: function(obj) {
				layer.load();
				var checkStatus = table.checkStatus('tableBizLayer'); //idTest 即为基础参数 id 对应的值
				if (checkStatus!=null &&  checkStatus.data && checkStatus.data.length>0) {
					this.data = {
						id: function() {
							return checkStatus.data[0].id;
						}
					}
				}
			},
			done: function(res) {
				layer.closeAll('loading');
				if (res.code == '200') {
					layer.msg(res.message, {
						icon: 1
					});
					insTb.reload({}, 'data');
				} else {
					layer.msg(res.message, {
						icon: 2
					});
				}
			},
			error: function(index, upload) {
				layer.closeAll('loading');
				layer.msg("上传失败！", {
					icon: 2
				});
			}
		});
        
        // 删除
        function doDel(data) {
            layer.confirm('确定要删除“【<span style="color:red;">' + data.name + '</span>】”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (index) {
                layer.close(index);
                layer.load(2);
                admin.req('biz/layer/delete', {
                    id: data.id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.message, {icon: 1});
                        insTb.reload({}, 'data');
                    } else {
                        layer.msg(res.message, {icon: 2});
                    }
                }, 'get');
            });
        }

        // 显示表单弹窗
        function showEditModel(Layer) {
            admin.open({
                type: 1,
                title: (Layer ? '修改' : '添加'),
                content: $('#modelLayer').html(),
                offset: 't'	,
                area:'400px',
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    var url = 'biz/layer/save';
                    form.val('modelLayerForm', Layer);  // 回显数据
                    // 表单提交事件
                    form.on('submit(modelSubmitLayer)', function (data) {
                        layer.load(2);
                        var formData={}; 
                        for(var key in data.field){
                        	formData['bizLayer.'+key]=data.field[key];
                        }
                        admin.req(url,formData, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.message, {icon: 1});
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(res.message, {icon: 2});
                            }
                        }, 'get');
                        return false;
                    });
                }
            });
        }

    });
</script>